In [14]:
library("bnstruct")
library("dagR")
In [2]:
count <- function(x) {
return(length(unique(x)))
}
quantize <- function(x) {
return(x - min(x) + 1)
}
In [18]:
# Given model:
#
# A -> B
# A -> C
# B -> D
# C -> D
#
SIZE <- 1000
A <- round(rnorm(SIZE, 10, 2))
B <- round(0.4 * A + rnorm(SIZE, 10, 1))
C <- round(0.8 * A + rnorm(SIZE, 10, 1.5))
D <- round(0.4 * B + 0.7 * C + rnorm(SIZE, 10, 1))
data = cbind(A, B, C, D)
data = apply(data, 2, quantize)
dataset <- BNDataset(data = as.matrix(data),
discreteness = rep('D', 4),
variables = c("A", "B", "C", "D"),
node.sizes = apply(data, 2, count))
dataset_bs <- bootstrap(dataset, num.boots = 1000)
In [19]:
net <- learn.network(dataset, scoring.func = "BDeu", algo="SEM")
dag(net)
plot(net)
In [25]:
net <- learn.network(dataset_bs, bootstrap = TRUE)
wpdag(net)
#plot(net, plot.wpdag=T)
plot(net)
In [22]:
# Given model:
#
# A -> B
# A -> C
# B -> D
# C -> D
#
SIZE <- 1000
A <- round(rnorm(SIZE, 10, 2))
B <- round(0.4 * A + rnorm(SIZE, 10, 1))
C <- round(0.8 * A + rnorm(SIZE, 10, 1.5))
D <- round(0.4 * B + 0.2 * C + rnorm(SIZE, 10, 1))
data = cbind(A, B, C, D)
data = apply(data, 2, quantize)
dataset <- BNDataset(data = as.matrix(data),
discreteness = rep('D', 4),
variables = c("A", "B", "C", "D"),
node.sizes = apply(data, 2, count))
dataset_bs <- bootstrap(dataset, num.boots = 1000)
In [23]:
net <- learn.network(dataset, scoring.func = "BDeu", algo="SEM")
dag(net)
plot(net)
In [ ]: